﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Caching;
using System.Web.UI.WebControls;
using Controls;
using Model;

namespace FrontEnd.forms
{
    public partial class RepTitles : Page
    {
        private Functions _functions;

        private IEnumerable<ReputationResult> ReputationResults
        {
            get
            {
                var reputationResults = Cache["ReputationResult"] as List<ReputationResult>;
                if (reputationResults == null)
                {
                    reputationResults = _functions.GetReputationStatistics();
                    Cache.Insert("ReputationResult", reputationResults, new CacheDependency(Conf.App("ResetCachePath")));
                }
                return reputationResults;
            }
        }

        private IEnumerable<ReputationResult> Factions
        {
            get
            {
                var singleFactionResult = Cache["SingleFactionResult"] as IEnumerable<ReputationResult>;
                if (singleFactionResult == null)
                {
                    singleFactionResult = (from reputationResult in ReputationResults
                                           where reputationResult.Hated > 0 || reputationResult.Hostile > 0 || reputationResult.Unfriendly > 0 || reputationResult.Neutral > 0 ||
                                                 reputationResult.Friendly > 0 || reputationResult.Honored > 0 || reputationResult.Revered > 0 || reputationResult.Exalted > 0
                                           select reputationResult).ToList();
                    Cache.Insert("SingleFactionResult", singleFactionResult, new CacheDependency(Conf.App("ResetCachePath")));
                }
                return singleFactionResult;
            }
        }

        public bool SortAscending
        {
            get { return Session["SortAscending"] == null || bool.Parse(Session["SortAscending"].ToString()); }
            set { Session["SortAscending"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _functions = new Functions(Conf.Db("CrazyWowStatsEntities"));
            FactionsGridView.DataSource = Factions.ToList();
            FactionsGridView.DataBind();
        }

        protected void FactionsGridViewSorting(object sender, GridViewSortEventArgs e)
        {
            IEnumerable<ReputationResult> sortedList = Factions;

            switch (e.SortExpression)
            {
                case Constants.Reputation.Hated:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Hated).ToList() : Factions.OrderByDescending(f => f.Hated).ToList());
                    break;
                case Constants.Reputation.Hostile:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Hostile).ToList() : Factions.OrderByDescending(f => f.Hostile).ToList());
                    break;
                case Constants.Reputation.Unfriendly:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Unfriendly).ToList() : Factions.OrderByDescending(f => f.Unfriendly).ToList());
                    break;
                case Constants.Reputation.Neutral:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Neutral).ToList() : Factions.OrderByDescending(f => f.Neutral).ToList());
                    break;
                case Constants.Reputation.Friendly:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Friendly).ToList() : Factions.OrderByDescending(f => f.Friendly).ToList());
                    break;
                case Constants.Reputation.Honored:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Honored).ToList() : Factions.OrderByDescending(f => f.Honored).ToList());
                    break;
                case Constants.Reputation.Revered:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Revered).ToList() : Factions.OrderByDescending(f => f.Revered).ToList());
                    break;
                case Constants.Reputation.Exalted:
                    sortedList = (SortAscending ? Factions.OrderBy(f => f.Exalted).ToList() : Factions.OrderByDescending(f => f.Exalted).ToList());
                    break;
            }

            FactionsGridView.DataSource = sortedList;
            FactionsGridView.DataBind();
        }

        protected void FactionsGridViewPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            FactionsGridView.PageIndex = e.NewPageIndex;
            FactionsGridView.DataBind();
        }

        protected void FactionsGridViewSorted(object sender, EventArgs e)
        {
            SortAscending = !SortAscending;
        }
    }
}